/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Crawler;

import DBSupport.DBApi;
import DataStructures.Client;
import DataStructures.Project;
import General.Globals;
import LinkRetriever.DataManager;
import LinkRetriever.Link;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Manuel
 */
public class ClientHandler {

    private static ClientHandler handler = new ClientHandler();

    public static ClientHandler getInstance(){

        return handler;
    }

    private ArrayList<Client> currentclients;
    private DataManager dmgmt;
    private SecureRandom random;
    private Scheduler sch;
    private int counter;

    private ClientHandler(){
        System.getProperties().put("proxySet", "true");
        System.getProperties().put("proxyPort", "8080");
        System.getProperties().put("proxyHost", "passage.inf.unibz.it");
        currentclients = new ArrayList<Client>();
        dmgmt = DataManager.getInstance();
        random = new SecureRandom();
        counter = 0;
    }

    public  String getNewLink(){
        counter++;
        if(counter > Globals.LIST_UPDATE){
            for(int i = 0;i<currentclients.size();i++){
                Client c = currentclients.get(i);
                if((System.currentTimeMillis() - c.getTimestamp().getTime())> 80000000l){
                    dmgmt.freeLink(c.getUrl());
                    currentclients.remove(i);
                }
            }
        }
        String code = this.generateCode();
        Link l = dmgmt.getLink();
        /*String code = "random";
        String link = "http://sourceforge.net/projects/jcollectit/";*/
        System.out.println("Searching for new link, link:"+l.getUrl());
        Client c = new Client();
        c.setCode(code);
        c.setTimestamp(new Date(System.currentTimeMillis()));
        c.setUrl(l.getUrl());
        this.currentclients.add(c);
        System.out.println("Added new client.");
        String response = "1;"+code+";"+l.getUrl()+";"+l.getFromRevision();
        System.out.println("Returning Response: "+response);
        return response;
    }

    public String storeProject(Project p, String code){
        for(Client c : currentclients){
            if(c.getUrl().toLowerCase().equals(p.getProjectInfo().getUrl().toLowerCase())){
                if(code.equals(c.getCode())){
                    dmgmt.storeProject(p);
                    return "3;Success. Thank you, project updated.";
                }
            }
        }
        return "3;Failure. Client not recognized.";
    }

    public String crawlNewLinks(String password, String limit){
        System.out.println(password+","+Globals.PASSWORD);
        if(password.equals(Globals.PASSWORD)){
            Scheduler sch = new Scheduler("sourceforge,google",limit);
            sch.startCrawler();
            return "5;Success. Crawling for new links.";
        }
        return "5;Failure. Client not recognized.";
        
    }

    private String generateCode(){
        return new BigInteger(130, random).toString(32);
    }

    private String hashCode(String s){
        return s;
    }

}
